package com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.repository.HddcWyVolcanicsvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.repository.HddcWyVolcanicsvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.repository.entity.HddcWyVolcanicsvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.service.HddcWyVolcanicsvypointService;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.viewobjects.HddcWyVolcanicsvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSvyPoint.viewobjects.HddcWyVolcanicsvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-02
 */
@Service
public class HddcWyVolcanicsvypointServiceImpl implements HddcWyVolcanicsvypointService {

	@Autowired
    private HddcWyVolcanicsvypointRepository hddcWyVolcanicsvypointRepository;
    @Autowired
    private HddcWyVolcanicsvypointNativeRepository hddcWyVolcanicsvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyVolcanicsvypoints(HddcWyVolcanicsvypointQueryParams queryParams , int curPage,
                                                   int pageSize) {
        Page<HddcWyVolcanicsvypointEntity> hddcWyVolcanicsvypointPage =
                this.hddcWyVolcanicsvypointNativeRepository.queryHddcWyVolcanicsvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyVolcanicsvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyVolcanicsvypointEntity getHddcWyVolcanicsvypoint(String id) {
        HddcWyVolcanicsvypointEntity hddcWyVolcanicsvypoint = this.hddcWyVolcanicsvypointRepository.findById(id).orElse(null);
         return hddcWyVolcanicsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyVolcanicsvypointEntity saveHddcWyVolcanicsvypoint(HddcWyVolcanicsvypointEntity hddcWyVolcanicsvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyVolcanicsvypoint.setId(uuid);
        hddcWyVolcanicsvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyVolcanicsvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyVolcanicsvypointRepository.save(hddcWyVolcanicsvypoint);
        return hddcWyVolcanicsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyVolcanicsvypointEntity updateHddcWyVolcanicsvypoint(HddcWyVolcanicsvypointEntity hddcWyVolcanicsvypoint) {
        hddcWyVolcanicsvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyVolcanicsvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyVolcanicsvypointRepository.save(hddcWyVolcanicsvypoint);
        return hddcWyVolcanicsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyVolcanicsvypoints(List<String> ids) {
        List<HddcWyVolcanicsvypointEntity> hddcWyVolcanicsvypointList = this.hddcWyVolcanicsvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyVolcanicsvypointList) && hddcWyVolcanicsvypointList.size() > 0) {
            for(HddcWyVolcanicsvypointEntity hddcWyVolcanicsvypoint : hddcWyVolcanicsvypointList) {
                this.hddcWyVolcanicsvypointRepository.delete(hddcWyVolcanicsvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
